package com.fw.controller;

import com.alibaba.fastjson.JSONObject;
import com.fw.entity.*;
import com.fw.enums.PigCheckState;
import com.fw.enums.PigOState;
import com.fw.service.IPigService;
import com.fw.service.IPigorderService;
import com.fw.service.IUserService;
import com.fw.utils.JWTUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;


@RequestMapping("/feeder")
@RestController
public class FeederController {
    @Resource
    IPigService pigService;
    @Resource
    IPigorderService pigorderService;
    @Resource
    private IUserService userService;

    @RequestMapping("/addPig")
    public Result addPig(@RequestParam int num,
                         HttpServletRequest request)throws MyException {
        String pigraiseuid = JWTUtils.getUserId(request.getHeader("token"));
        System.out.println(pigraiseuid);
        List<Pig>pigs = new ArrayList<>();
        for(int i=0;i<num;i++){
            Pig pig =new Pig();
            pig.setPigraiseuid(new Integer(pigraiseuid));
            pig.setPigraisecheck("1");
            pig.setPigkillcheck("1");
            pig.setPigstate("1");
            pigs.add(pig);
        }
        boolean ifin = pigService.saveOrUpdateBatch(pigs);
        if(ifin){
            return new Result(null,"该养殖场生猪数量增加成功","000000");
        }
        else{
            throw new MyException("生猪增加失败，请重新尝试","020601");
        }
    }

    @RequestMapping("/listAllPigOrder")
    public Result listAllPigOrder(HttpServletRequest request)throws MyException{
        String pigraiseuid = JWTUtils.getUserId(request.getHeader("token"));
        Map<String, Object> m = new HashMap<>();
        m.put("pigraiseuid", pigraiseuid);
        System.out.println(m);
        List<Pigorder> pigorders = pigorderService.listByMap(m);
        int size = pigorders.size();
        m.clear();
        List<JSONObject> list = new ArrayList<>();
        for(int i=0;i<size;i++){
            JSONObject jsonObject1i = new JSONObject();
            Pigorder pigorder = pigorders.get(i);
            int uid = pigorder.getPigkilluid();
            m.put("uid",uid);
            User user = userService.listByMap(m).get(0);
            String username = user.getUname();
            jsonObject1i.put("pigKillName",username);
            jsonObject1i.put("pigOrder",pigorder);
            m.clear();
            m.put("pigoid", pigorder.getPigoid());
            List<Pig> pigList = pigService.listByMap(m);
            pigorder.setPigList(pigList);
            list.add(jsonObject1i);
            m.clear();
        }
        return new Result(list,"获取全部生猪订单成功","000000");
    }

    @RequestMapping("/getPigOrder")
    public Result getPigOrder(int pigoid,HttpServletRequest request
                              )throws MyException{
        String pigraiseuid = JWTUtils.getUserId(request.getHeader("token"));
        Map<String, Object> m = new HashMap<>();
        if((pigoid + "").equals("")){
            throw new MyException("订单号不能为空", "020201");
        }
        //if(pigoid instanceof Interger){
        m.put("pigoid", pigoid);
        //m.put("pigraiseuid", pigraiseuid);
        System.out.println(m);
        List<Pigorder> pigorders = pigorderService.listByMap(m);
        if(pigorders.isEmpty()){
            throw new MyException("该订单不存在", "020202");
        }

        List<Pig>pigs = pigService.listByMap(m);
        Pigorder np = pigorders.get(0);
        int killuid = np.getPigkilluid();
        np.setPigList(pigs);
        m.clear();
        m.put("uid",killuid);
        User user = userService.listByMap(m).get(0);
        String username = user.getUname();
        JSONObject jsonObject1i = new JSONObject();
        jsonObject1i.put("pigkillname",username);
        jsonObject1i.put("pigOrder",np);
        return new Result(jsonObject1i,"获取某个生猪订单成功","000000");
        //}
        //else{
            //throw new MyException("订单id数据类型错误", "020203");
        //}
    }

    @RequestMapping("/pigOut")
    public Result pigOut(String pigoid,HttpServletRequest request
                         )throws MyException{
        String pigraiseuid = JWTUtils.getUserId(request.getHeader("token"));
        Map<String, Object> m = new HashMap<>();
        if(pigoid.equals("")){
            throw new MyException("订单号不能为空", "020302");
        }
        m.put("pigoid", pigoid);
        System.out.println(m);
        List<Pigorder> pigorders = pigorderService.listByMap(m);
        if(pigorders.isEmpty()){
            throw new MyException("该订单不存在", "020301");
        }
        Pigorder pigorder = pigorders.get(0);
        System.out.println(pigorder.getPigraiseuid() + " " + pigraiseuid);
        if(!pigorder.getPigraiseuid().toString().equals(pigraiseuid))
            throw new MyException("该订单不属于该用户", "020304");
        if(!pigorder.getPigostate().equals(PigOState.START.getValue()))
            throw new MyException("该订单已出场", "020303");
        pigorder.setOutdate(new Date());
        pigorder.setPigostate(PigOState.OUT.getValue());
        pigorderService.saveOrUpdate(pigorder);
        return new Result(null,"生猪出场成功","000000");
    }

    @RequestMapping("/listAllPig")
    public Result listAllPig(HttpServletRequest request)throws MyException{
        String pigraiseuid = JWTUtils.getUserId(request.getHeader("token"));
        Map<String, Object> m = new HashMap<>();
        m.put("pigraiseuid", pigraiseuid);
        List<Pig> pigs = pigService.listByMap(m);
        if(pigs.isEmpty()){
            throw  new MyException("该用户没有猪","020401");
        }
        else{
            return new Result(pigs,"获取生猪列表成功","000000");
        }
    }

    @RequestMapping("/raiseCheck")
    public Result raiseCheck(HttpServletRequest request)throws MyException{
        String pigraiseuid = JWTUtils.getUserId(request.getHeader("token"));
        Map<String, Object> m = new HashMap<>();
        m.put("pigraiseuid", pigraiseuid);
        List<Pig> pigs = pigService.listByMap(m);
        int size = pigs.size();
        int num = 0;
        for (Pig pig1 : pigs) {
            if (pig1.getPigraisecheck().equals("1")) {
                pig1.setPigraisecheck(PigCheckState.CHECKING.getValue());
                num++;
            }
        }
        if(num==0){
            throw new MyException("该用户没有猪待检疫","020501");
        }
        pigService.saveOrUpdateBatch(pigs);
        return new Result(null,"提交生猪检疫成功，共提交" + num + "头猪","000000");
    }
}
